# Wrangle data for plotting
iris_bar_df <- iris %>%
group_by(Species) %>%
summarise(Sepal_Width = mean(Sepal.Width)) %>%
ungroup()
# Create basic bar chart (left)
plotly_iris_bar <- plot_ly(iris_bar_df,
x = ~Species,
y = ~Sepal_Width,
type = "bar")
# Create formatted chart (right)
plotly_iris_bar_formatted <- plot_ly(iris_bar_df,
x = ~Species,
y = ~Sepal_Width,
type = "bar",
marker = list(color = nice_colours[["bold_teal_100"]],
line = list(color = nice_colours[['black_100']], width = 1.5)))
plotly_iris_bar
plotly_iris_bar_formatted
# fig.show = "hold" not working for Plotly?
plotly_iris_bar_themed <- plotly_iris_bar_formatted %>%
nice_plotly_theme(chart_type = "vertical_bar",
x_title = "Species",
y_title = "Sepal Width")
Alt text (text description of message chart is showing. Mark chart as
decorative)
Source: []
Download the data for Figure x (CSV, 5.0KB).
To download the plot as a static PNG image, click on the camera icon at the top right of the plot.
Saving in other file formats (e.g. .svg or .jpg) is more complicated and requires installation of another piece of software. Read the Plotly documentation on exporting graphs as static images in R.
# Load in DOACs data
load(file = here::here("data", "doacs_df.rda"))
Use chart_type = “horizontal_bar” for histograms.
# Create chart
plotly_hist <- iris %>%
plot_ly(x = ~Sepal.Width,
color = ~Species,
# Use NICE colours - remove colour names so it maps correctly
colors = unname(nice_palettes[["discrete"]][1:3]),
type = "histogram",
# Black outline for bars
marker = list(line = list(color = nice_colours[['black_100']], width = 1.5)),
# Set bin width
xbins = list(size = 0.2,
start = 1.9,
end = 4.5)) %>%
# Stack bars
layout(barmode = "stack") %>%
# Add NICE theme and set axis titles
nice_plotly_theme(chart_type = "vertical_bar",
x_title = "Sepal width",
y_title = "Frequency")
plotly_hist
# Category order differs from ggplot but no big difference
Use chart_type = “vertical_bar”.
# Prepare data, get total items dispensed for each medicine in 2021 (in millions)
bar_df <- doacs_df %>%
filter(between(date, as.Date("2021-01-01"), as.Date("2021-12-31"))) %>%
group_by(chemical) %>%
summarise(items = sum(items)/1000000) %>%
ungroup()
# Create chart
plotly_vbar_chart <- bar_df %>%
# Reorder chemical factor levels in decreasing order of items
mutate(chemical = reorder(chemical, -items)) %>%
plot_ly(x = ~chemical,
y = ~items,
type = "bar",
# Bold teal bars with black outline
marker = list(color = nice_colours[["bold_teal_100"]],
line = list(color = nice_colours[['black_100']], width = 1.5))) %>%
# Add NICE theme and set axis titles
nice_plotly_theme(chart_type = "vertical_bar",
x_title = "",
y_title = "Dispensed items (millions)")
Apixaban was the most prescribed DOAC in 2021
Figure x. Total DOAC medicines dispensed in primary care in England, 2020
Use chart_type = “horizontal_bar”.
# Create chart
plotly_vbar_chart <- bar_df %>%
# Reorder chemical factor levels in decreasing order of items
mutate(chemical = reorder(chemical, items)) %>%
plot_ly(x = ~items,
y = ~chemical,
type = "bar",
# Make horizontal
orientation = "h",
# Bold teal bars with black outline
marker = list(color = nice_colours[["bold_teal_100"]],
line = list(color = nice_colours[['black_100']], width = 1.5))) %>%
# Add NICE theme and set axis titles
nice_plotly_theme(chart_type = "horizontal_bar",
x_title = "Dispensed items (millions)",
y_title = "")
plotly_vbar_chart
Use chart_type = “line”.
# Prepare data
line_df <- doacs_df %>%
filter(chemical == "Edoxaban") %>%
group_by(date, chemical) %>%
summarise(items = sum(items)) %>%
ungroup()
# Create chart
plotly_line_chart <- line_df %>%
plot_ly(x = ~date,
y = ~items,
type = "scatter",
# Line with marker dot at each data point
mode = "lines+markers",
# Make line and dots teal
marker = list(color = nice_colours[["bold_teal_100"]])) %>%
# Add the vertical dashed line to show lockdown date. Added this before plotting the
# data so that it sits on a layer behind the line and points
add_lines(x = as.Date("2020-03-01"),
y = ~c(0, max(items)),
line = list(dash = "dash",
color = "#000000",
width = 1.5),
# Don't inherit properties of previous trace
inherit = FALSE) %>%
layout(showlegend = FALSE,
# Y axis ticks with commas as thousands separators
xaxis = list(type = "date", # Specify x axis is date
# Show x axis ticks
ticks = "outside",
# Format ticks as abbreviated month name and full year, e.g. Jan 2018
tickformat = "%b\n%Y",
# Set first tick
tick0 = "2017-07-01",
# Tick every 6 months
dtick = "M6"),
yaxis = list(tickformat = ",",
range = ~c(0, max(items)+10000)),
annotations = list(x = as.Date("2020-03-01"),
xshift = -60,
y = 180000,
text = "National lockdown\non 23 March",
xref = "x",
yref = "y",
showarrow = FALSE,
align = "right")) %>%
# Add NICE theme and set axis titles
nice_plotly_theme(chart_type = "line",
x_title = "",
y_title = "Dispensed items",
# Don't pad the axes, as ticks shown. Do not want gap between axis line and ticks
pad_axes = FALSE)
Edoxaban prescribing has increased since 2017
Figure x. Edoxaban prescribing in England, 2017-2022
economics_long %>%
filter(variable != "pop") %>%
plot_ly(x = ~date,
y = ~value01,
color = ~variable,
colors = c("#00436C", "#D07B4D", "#000000", "#37906D"),
type = "scatter",
mode = "lines") %>%
nice_plotly_theme(chart_type = "line")
plot1 <- mpg %>%
filter(manufacturer %in% c("hyundai", "nissan", "toyota")) %>%
count(manufacturer) %>%
plot_ly(y = ~manufacturer,
x = ~n,
color = ~manufacturer,
type = "bar",
orientation = "h",
marker = list(line = list(color = '#000000', width = 1.5)))
nice_plotly_theme(plot1, chart_type = "horizontal_bar")
mpg %>%
filter(manufacturer %in% c("hyundai", "nissan", "toyota")) %>%
count(manufacturer) %>%
plot_ly(y = ~manufacturer,
x = ~n,
color = ~manufacturer,
type = "bar",
orientation = "h",
marker = list(line = list(color = '#000000', width = 1.5))) %>%
nice_plotly_theme(chart_type = "horizontal_bar")
mpg %>%
filter(manufacturer %in% c("hyundai", "nissan", "toyota")) %>%
count(manufacturer) %>%
plot_ly(x = ~manufacturer,
y = ~n,
color = ~manufacturer,
type = "bar",
marker = list(line = list(color = '#000000', width = 1.5))) %>%
nice_plotly_theme(chart_type = "vertical_bar")